/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var maxUncrossedLines = function (nums1, nums2) {
  const len1 = nums1.length
  const len2 = nums2.length
  const dp = new Array(len1 + 1).fill(0).map(() => new Array(len2 + 1).fill(0))

  for (let i = 0; i <= len1; i++) {
    dp[i][0] = 0
  }
  for (let i = 0; i <= len2; i++) {
    dp[0][i] = 0
  }

  for (let i = 1; i <= len1; i++) {
    for (let j = 1; j <= len2; j++) {
      let p1 = 0
      let p2 = 0
      let p3 = 0
      if (nums1[i - 1] === nums2[j - 1]) {
        p1 = dp[i - 1][j - 1] + 1
      }
      p2 = dp[i - 1][j]
      p3 = dp[i][j - 1]

      dp[i][j] = Math.max(p1, p2, p3)
    }
  }

  return dp[len1][len2]
}

console.log(
  maxUncrossedLines(
    [2, 5, 1, 2, 5], [10, 5, 2, 1, 5, 2]
  )
)
